package com.wejoy.video.middle.station.admin.controller.system;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.system.SysCarmera;
import com.wejoy.video.middle.station.data.domain.system.SysCarmeraConfig;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.enums.dict.EnumPointConfigType;
import com.wejoy.video.middle.station.data.enums.dict.EnumYesNo;
import com.wejoy.video.middle.station.data.exception.BusinessException;
import com.wejoy.video.middle.station.data.service.evs.IEvsHttpService;
import com.wejoy.video.middle.station.data.service.system.ISysCarmeraConfigService;
import com.wejoy.video.middle.station.data.util.DATAUtil;
import com.wejoy.video.middle.station.data.util.DateUtils;
import com.wejoy.video.middle.station.data.util.StringUtils;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.vo.api.BusinessPointConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;

/**
 * 算法配置
 *
 * @author miracle_j
 * @date 2024年04月25日 16:10:43
 */
@RestController
@RequestMapping("/system/sysCarmeraConfig")
public class SysCarmeraConfigController extends BaseController {

    @Autowired
    private ISysCarmeraConfigService sysCarmeraConfigService;

    @Autowired
    private IEvsHttpService evsHttpService;


    /**
     * 获取点位某个算法配置
     *
     * @param sysCarmera
     * @return RespJSON
     */
    @PostMapping("/getAlgorithmConfig")
    public RespJSON getAlgorithmConfig(@RequestBody SysCarmeraConfig sysCarmera) {
        validateSysCarmera(sysCarmera);
        SysCarmeraConfig sysCarmeraConfig = sysCarmeraConfigService.getOneByIdAndSceneTypeAndAbilityType(sysCarmera.getPointId(), sysCarmera.getSceneType(), sysCarmera.getAbilityType());
        if(ObjectUtils.isEmpty(sysCarmeraConfig)){
            sysCarmeraConfig = new SysCarmeraConfig();
            JSONObject pointConfig = evsHttpService.getPointConfig(sysCarmera.getSceneType(), sysCarmera.getAbilityType(), sysCarmera.getPointId().intValue(), evsHttpService.getToken());
            if(ObjectUtils.isEmpty(pointConfig)){
                throw new BusinessException("点位不存在或未配置相关算法");
            }
            sysCarmeraConfig.setConfigType(EnumPointConfigType.WEEK.getCode());
            sysCarmeraConfig.setEffectiveTime(pointConfig.getJSONObject("config").getList("effectiveTime",BusinessPointConfigVO.ConfigBean.EffectiveTimeBean.class));
            sysCarmeraConfig.setSkipHolidays(false);
            sysCarmeraConfig.setSeasonStatus(false);
            sysCarmeraConfig.setSceneType(sysCarmera.getSceneType());
            sysCarmeraConfig.setAbilityType(sysCarmera.getAbilityType());
            sysCarmeraConfig.setPointId(sysCarmera.getPointId());
            sysCarmeraConfig.setConfigJson(pointConfig.getJSONObject("config").getJSONArray("effectiveTime").toJSONString());
            sysCarmeraConfigService.save(sysCarmeraConfig,"初始化");
        }
        sysCarmeraConfig.setWinterEffectiveTime(JSONArray.parseArray(sysCarmeraConfig.getWinterConfigJson(),BusinessPointConfigVO.ConfigBean.EffectiveTimeBean.class));
        sysCarmeraConfig.setEffectiveTime(JSONArray.parseArray(sysCarmeraConfig.getConfigJson(),BusinessPointConfigVO.ConfigBean.EffectiveTimeBean.class));
        return RespJSON.success(sysCarmeraConfig);
    }

    /**
     * 更改点位某个算法配置
     *
     * @param sysCarmera
     * @return RespJSON
     */
    @PostMapping("/setAlgorithmConfig")
    @RoleCheck(name = "algorithm_edit")
    public RespJSON setAlgorithmConfig(@RequestBody SysCarmeraConfig sysCarmera,@SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        validateSysCarmera(sysCarmera);
        SysCarmeraConfig sysCarmeraConfig = sysCarmeraConfigService.getOneByIdAndSceneTypeAndAbilityType(sysCarmera.getPointId(), sysCarmera.getSceneType(), sysCarmera.getAbilityType());
        if(ObjectUtils.isEmpty(sysCarmeraConfig)){
            throw new BusinessException("点位不存在");
        }
        JSONObject pointConfig = evsHttpService.getPointConfig(sysCarmera.getSceneType(), sysCarmera.getAbilityType(), sysCarmera.getPointId().intValue(), evsHttpService.getToken());
        if(ObjectUtils.isEmpty(pointConfig)){
            throw new BusinessException("点位不存在或未配置相关算法");
        }
        BusinessPointConfigVO businessPointConfigVO = JSONObject.parseObject(pointConfig.toJSONString(), BusinessPointConfigVO.class);
        sysCarmera.setId(sysCarmeraConfig.getId());
        //如果是按天配置
        if(sysCarmera.getConfigType().equals(EnumPointConfigType.DAY.getCode())){
            if(StringUtils.isEmpty(sysCarmera.getBeginTime())||StringUtils.isEmpty(sysCarmera.getEndTime())){
                throw new BusinessException("时间配置不能为空！");
            }
            ArrayList<BusinessPointConfigVO.ConfigBean.EffectiveTimeBean> effectiveTimeBeans = new ArrayList<>();
            for (int i = 1; i < 8; i++) {
                BusinessPointConfigVO.ConfigBean.EffectiveTimeBean effectiveTimeBean = new BusinessPointConfigVO.ConfigBean.EffectiveTimeBean();
                effectiveTimeBean.setBeginTime(sysCarmera.getBeginTime());
                effectiveTimeBean
                        .setEndTime(sysCarmera.getEndTime());
                effectiveTimeBean.setDayOfWeek(i);
                effectiveTimeBeans.add(effectiveTimeBean);
            }
            sysCarmera.setEffectiveTime(effectiveTimeBeans);
            sysCarmera.setConfigJson(JSONObject.toJSONString(effectiveTimeBeans));
            //如果开启了季节配置
            if(sysCarmera.getSeasonStatus()){
                if(StringUtils.isEmpty(sysCarmera.getWinterBeginTime())||StringUtils.isEmpty(sysCarmera.getWinterEndTime())){
                    throw new BusinessException("冬季时间配置不能为空！");
                }
                ArrayList<BusinessPointConfigVO.ConfigBean.EffectiveTimeBean> wintereffectiveTimeBeans = new ArrayList<>();
                for (int i = 1; i < 8; i++) {
                    BusinessPointConfigVO.ConfigBean.EffectiveTimeBean effectiveTimeBean = new BusinessPointConfigVO.ConfigBean.EffectiveTimeBean();
                    effectiveTimeBean.setBeginTime(sysCarmera.getWinterBeginTime());
                    effectiveTimeBean.setEndTime(sysCarmera.getWinterEndTime());
                    effectiveTimeBean.setDayOfWeek(i);
                    wintereffectiveTimeBeans.add(effectiveTimeBean);
                }
                sysCarmera.setWinterEffectiveTime(wintereffectiveTimeBeans);
                sysCarmera.setWinterConfigJson(JSONObject.toJSONString(wintereffectiveTimeBeans));
            }
            List<Integer> objects = new ArrayList<>();
            objects.add(sysCarmera.getPointId().intValue());
            businessPointConfigVO.setPointIds(objects);
            businessPointConfigVO.getConfig().setEffectiveTime(sysCarmera.getEffectiveTime());
            if(sysCarmera.getSeasonStatus()&&!DateUtils.isSummer()){
                businessPointConfigVO.getConfig().setEffectiveTime(sysCarmera.getWinterEffectiveTime());
            }
            JSONObject jsonObject = evsHttpService.businessHallPointConfig(sysCarmera.getSceneType(), sysCarmera.getAbilityType(), businessPointConfigVO, evsHttpService.getToken());
            if(!ObjectUtils.isEmpty(jsonObject)){
                throw new BusinessException("调用api配置错误");
            }
            sysCarmeraConfigService.save(sysCarmera,loginSysUser.getName());
        }
        //如果是按周配置
        if(sysCarmera.getConfigType().equals(EnumPointConfigType.WEEK.getCode())){
            if(CollectionUtils.isEmpty(sysCarmera.getEffectiveTime())){
                throw new BusinessException("时间配置不能为空！");
            }
            sysCarmera.setConfigJson(JSONObject.toJSONString(sysCarmera.getEffectiveTime()));
            if(sysCarmera.getSeasonStatus()&&CollectionUtils.isEmpty(sysCarmera.getWinterEffectiveTime())){
                throw new BusinessException("冬季时间配置不能为空！");
            }
            if(sysCarmera.getSeasonStatus()){
                sysCarmera.setWinterConfigJson(JSONObject.toJSONString(sysCarmera.getWinterEffectiveTime()));
            }
            List<Integer> objects = new ArrayList<>();
            objects.add(sysCarmera.getPointId().intValue());
            businessPointConfigVO.setPointIds(objects);
            businessPointConfigVO.getConfig().setEffectiveTime(sysCarmera.getEffectiveTime());
            if(sysCarmera.getSeasonStatus()&&!DateUtils.isSummer()){
                businessPointConfigVO.getConfig().setEffectiveTime(sysCarmera.getWinterEffectiveTime());
            }
            JSONObject jsonObject = evsHttpService.businessHallPointConfig(sysCarmera.getSceneType(), sysCarmera.getAbilityType(), businessPointConfigVO, evsHttpService.getToken());
            if(!ObjectUtils.isEmpty(jsonObject)){
                throw new BusinessException("调用api配置错误");
            }
            sysCarmeraConfigService.save(sysCarmera,loginSysUser.getName());
        }
        return RespJSON.success();
    }

    private void validateSysCarmera(SysCarmeraConfig sysCarmera) {
        WJAssert.notNull(sysCarmera, "入参对象不能为空");
        WJAssert.notNull(sysCarmera.getPointId(),"点位id不能为空!");
        WJAssert.notNull(sysCarmera.getSceneType(),"算法场景类型不能为空!");
        WJAssert.notNull(sysCarmera.getAbilityType(),"算法能力类型不能为空!");
    }


}
